// TODO: CODE REVIEW & CLEANUP!
//
// ServerSimulator.cs
//
// Implements ServerSimulator, which simulates a specific HomeUX server installation, with a
// specific configuration file, set of device drivers, and simulated hardware.
//

using System.IO;
using System.Linq;
using System.Reflection;
using HomeUX.SimulationOnClientSupport;
using HomeUX.Utilities;

namespace HomeUX.SimulationOnClient
{

public class ServerSimulator : ServerSimulatorBase
{
    ///////////////////////////////////////////////////////////////////////////////////////////////
    // Constants
    //
    
    /// <summary>
    /// The list of assemblies that are simulated by this assembly.
    /// </summary>
    ///
    static readonly string[] _simulatedAssemblies = new string[]
	{
        "AggregatorDriver.dll",
        "AquosDriver.dll",
        "AquosSimulator.dll",
        "AspDriver.dll",
        "AspSimulator.dll",
        "LutronDriver.dll",
		"JdsDriver.dll",
		"JdsSimulator.dll",
		"LutronSimulator.dll",
		"MusicPlayerDriver.dll",
		"MusicServerSimulator.dll",
        "PanasonicDriver.dll",
        "PanasonicSimulator.dll",
		"SunfireDriver.dll",
		"SunfireSimulator.dll"
	};

    ///////////////////////////////////////////////////////////////////////////////////////////////
    // Public Methods
    //

    /// <summary>
    /// Initializes an instance of this class.
    /// </summary>
    ///
    public ServerSimulator()
	{
        // register AssemblyResolver as a method that can return information about an assembly
        // given its name; we'll use this method to declare that this assembly is (effectively)
        // each of the assemblies listed in <_simulatedAssemblies>
        Util.RegisterAssemblyResolver(AssemblyResolver);
	}

    ///////////////////////////////////////////////////////////////////////////////////////////////
    // ServerSimulatorBase Overrides
    //

    public override Stream GetConfigStream()
    {
        return Assembly.GetExecutingAssembly().GetManifestResourceStream(
                "HomeUX.SimulationOnClient.App.config");
    }

    ///////////////////////////////////////////////////////////////////////////////////////////////
    // Private Methods
    //

    /// <summary>
    /// Returns the <n>Assembly</n> object corresponding to a given assembly path, or <n>null</n>
    /// if this resolver can't find the specified assembly.
    /// </summary>
    ///
    /// <param name="dllPath">The path to the assembly DLL (absolute, or relative to the the
    ///     directory containing the currently executing assembly).</param>
    ///
    Assembly AssemblyResolver(string dllPath)
    {
        if (_simulatedAssemblies.Contains(dllPath))
            return Assembly.GetExecutingAssembly();
        else
            return null;
    }
}

}
